package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.importRFC;


import java.io.File;
import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.database.DatabaseConnection;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.RFC;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.importRFCPanel.ImportRFCPanel;

public class ImportRFC extends Thread{
	
	private ImportRFCPanel importRFCPanel;
	private String path;
	public ImportRFC(ImportRFCPanel importRFCPanel, String path){
		this.path = importRFCPanel.getFunctionPanel().getPath();
		this.importRFCPanel = importRFCPanel;
	}
	
	
	@Override
	public void run() {
		importRFCPanel.getFunctionPanel().getLb_progressbar().setVisible(true);
		try {
			
			importRFC(path);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		importRFCPanel.getListPanel().getListModel().removeAllElements();
		importRFCPanel.getListPanel().getListModel().addElement("Alle Elemente importiert");
		importRFCPanel.getFunctionPanel().setPath("");
		importRFCPanel.getFunctionPanel().getLb_progressbar().setVisible(false);
	}


	public void importRFC(String path) throws JDOMException, IOException
	{
		File pathname= new File(path);
		if(pathname.isFile())
		{
			processXML(path);
		}
		else if (pathname.isDirectory()) 			
		{
			folderProcessing(path);
		}else {
			System.out.println("Worong File");
		}
	}
	
	private void processXML(String path) throws JDOMException, IOException
	{
		RFC rfc = new RFC("importetdID");
		rfc = xmlToRFC(path);
		writeRFCtoDB(rfc);
	}
	
	private void folderProcessing(String foldername) throws JDOMException, IOException
	{
		File folder = new File(foldername);
		String[]folderlist=folder.list();
		for(String xmlfile: folderlist)
		{
			processXML(foldername + xmlfile);
			
		}
	}

	private RFC xmlToRFC(String filename) throws JDOMException, IOException
	{
		RFC rfc = new RFC("importID");
		SAXBuilder builder = new SAXBuilder(false);
	    Document doc = builder.build(new File(filename));
	    Element root = doc.getRootElement();
		rfc.setAffected_Infrastructure_Components(root.getChildText(RFC.TC_AFFECTEDINFRASTRUCTURE));
		rfc.setAffected_Services(root.getChildText(RFC.TC_AFFECTEDSERVICES));
		rfc.setApproval_charge_Person(root.getChildText(RFC.TC_APPROVALPERSON));
		rfc.setBenefits(root.getChildText(RFC.TC_BENEFITS));
		rfc.setChange_Owner(root.getChildText(RFC.TC_CHANGEOWNER));
		rfc.setCosts(root.getChildText(RFC.TC_COSTS));
		rfc.setDate_Approval_Recjection(root.getChildText(RFC.TC_DATEOFREJECTIONORAPPROVAL));
		rfc.setDetailed_Description(root.getChildText(RFC.TC_DETAILEDDESCRIPTION));
		//rfc.setIdRFC(idRFC) ID wird von der DB gesetzt...
		rfc.setPriority(root.getChildText(RFC.TC_PRIORITY));
		rfc.setReason_for_Change(root.getChildText(RFC.TC_REASONFORCHANGE));
		rfc.setReason_of_Rejecting(root.getChildText(RFC.TC_REASONOFREJECTING));
		rfc.setRfc_Initiator(root.getChildText(RFC.TC_RFCINITIATOR));
		rfc.setRisks(root.getChildText(RFC.TC_RISKS));
		rfc.setStatus(root.getChildText(RFC.TC_Status));
		rfc.setSubmission_Date(root.getChildText(RFC.TC_SUBMISSIONDATE));
		rfc.setSummary(root.getChildText(RFC.TC_SUMMARY));   
		return rfc;	
	}
	
	private void writeRFCtoDB(RFC rfc)
	{
		DatabaseConnection connection = new DatabaseConnection();
		connection.connect();
		String query = "INSERT INTO CMS_RFC(" +
				RFC.TC_AFFECTEDINFRASTRUCTURE+", "+
				RFC.TC_AFFECTEDSERVICES+", "+
				RFC.TC_APPROVALPERSON+", "+
				RFC.TC_BENEFITS+", "+
				RFC.TC_CHANGEOWNER+", "+
				RFC.TC_COSTS+", "+
				RFC.TC_DATEOFREJECTIONORAPPROVAL+", "+
				RFC.TC_DETAILEDDESCRIPTION+", "+
				RFC.TC_PRIORITY+", "+
				RFC.TC_REASONFORCHANGE+", "+
				RFC.TC_REASONOFREJECTING+", "+
				RFC.TC_RFCINITIATOR+", "+
				RFC.TC_RISKS+", "+
				RFC.TC_Status+", "+
				RFC.TC_SUBMISSIONDATE+", "+
				RFC.TC_SUMMARY+
				") VALUES ('"+
				rfc.getAffected_Infrastructure_Components()+"','"+
				rfc.getAffected_Services()+"','"+
				rfc.getApproval_charge_Person()+"','"+
				rfc.getBenefits()+"','"+
				rfc.getChange_Owner()+"','"+
				rfc.getCosts()+"','"+
				rfc.getDate_Approval_Recjection()+"','"+
				rfc.getDetailed_Description()+"','"+
				rfc.getPriority()+"','"+
				rfc.getReason_for_Change()+"','"+
				rfc.getReason_of_Rejecting()+"','"+
				rfc.getRfc_Initiator()+"','"+
				rfc.getRisks()+"','"+
				//rfc.getSmallInformation()+"','"+
				rfc.getStatus()+"','"+
				rfc.getSubmission_Date()+"','"+
				rfc.getSummary()+"')";
		connection.insertquery(query);
		connection.disconnect();	
	}
}
